home *** CD-ROM | disk | FTP | other *** search
/ Electronic Clipper 1995 April / Electronic Clipper 1995-04.iso / pc / pc_users / ideasrc / setup / pviewer / viewmain.c < prev    next >
Text File  |  1993-04-17  |  19KB  |  509 lines

  1.  
  2. // ---------------------------------------------------------------------
  3. //
  4. // ViewMain.c - Picture Viewer - QuickTime for Windows
  5. //
  6. //              Version 1.0
  7. //
  8. //              (c) 1988-1992 Apple Computer, Inc. All Rights Reserved.
  9. //
  10. // ---------------------------------------------------------------------
  11.  
  12.  
  13. // Includes
  14. // --------
  15.    #include <Windows.h>                // Required by Windows
  16.  
  17.    #include <qtw.h>                    // Interface to QuickTime
  18.    #include <qtole.h>                  // Interface to qtole dll
  19.  
  20.    #include "common.h"                 // Interface to common.c routines
  21.  
  22.    #include "viewer.h"                 // Interface to other *.c files
  23.    #include "viewer.hr"                // Defines used in *.rc files
  24.    #include "picture.h"                // Needed for registering
  25.                                        // child window controls
  26.  
  27.  
  28. // Message-Persistent Data
  29. // -----------------------
  30.    static struct                        // Hungarian notation: g
  31.      {HINSTANCE     hInstance;          // Instance handle
  32.       HINSTANCE     hResources;         // Resource-only DLL handle
  33.       HWND          hwndFrame;          // Frame window handle
  34.       HWND          hwndClient;         // MDI client window
  35.       HMENU         hMenu;              // Frame window menu
  36.       HACCEL        hAccel;             // Frame window accelerators
  37.       HWND          hActiveModelessDlg; // Handle of active modeless dlg if any
  38.       BOOL          fPalettized;        // Palettized device
  39.       QTOLE_OLEDATA qtoleOleData;       // OLE data struct
  40.      } g;
  41.  
  42.  
  43. // Internal Function Declarations
  44. // ------------------------------
  45.    static LPSTR NEAR ViewerParseCmdLine    (LPSTR);
  46.    static  BOOL NEAR ViewerInitAppl        (VOID);
  47.    static  HWND NEAR ViewerInitInst        (HINSTANCE, LPSTR, int);
  48.    static  LONG NEAR ViewerTerminateInst   (VOID);
  49.    static  BOOL NEAR DoQuickTimeInit       (HINSTANCE, LPSTR, LPINT);
  50.    static  VOID NEAR KillQuickTime         (VOID);
  51.  
  52. // Function: WinMain - Required Windows "Main" Routine
  53. // --------------------------------------------------------------------
  54. // Parameters: As required by Microsoft Windows
  55. //
  56. // Returns:    As required by Microsoft Windows
  57. // --------------------------------------------------------------------
  58.    int PASCAL WinMain( HINSTANCE hInstance,
  59.                     HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  60.  
  61.    // Define function data
  62.  
  63.      {MSG  msg;                       // Message
  64.  
  65.       g.hInstance = hInstance;        // Initialize global data
  66.  
  67.    // Load resource-only DLL
  68.       if( !(g.hResources = CommonGetLocalizedResources
  69.                ( VIEWER_ROOT_NAME, hInstance, VIEWER_STRING_NOMEMORY )))
  70.          return 0;
  71.  
  72.    // Perform one-time initialization
  73.  
  74.       if( !hPrevInstance && !ViewerInitAppl() )
  75.          return 0;
  76.  
  77.    // Perform initializations that apply to a specific instance and
  78.    // create window
  79.       if( !ViewerInitInst( hPrevInstance, lpCmdLine, nCmdShow ))
  80.          return 0;
  81.  
  82.  
  83.     // main message loop
  84.       while( GetMessage( &msg, NULL, NULL, NULL ))
  85.          {if( !g.hActiveModelessDlg ||
  86.                         !IsDialogMessage( g.hActiveModelessDlg, &msg ))
  87.              {if( !g.hwndClient ||
  88.                      !TranslateMDISysAccel( g.hwndClient, &msg ))
  89.                  {if( !g.hwndFrame ||
  90.                       !TranslateAccelerator( g.hwndFrame, g.hAccel, &msg ))
  91.                      {TranslateMessage(&msg);
  92.                       DispatchMessage (&msg);
  93.                      }
  94.                  }
  95.              }
  96.          }
  97.  
  98.    // Cleanup Picture Viewer
  99.  
  100.       ViewerTerminateInst();
  101.  
  102.       return msg.wParam;
  103.      }
  104.  
  105.  
  106. // Function: ViewerInitAppl - Perform One-time Initialization
  107. // --------------------------------------------------------------------
  108. // Parameters: None
  109. //
  110. // Returns:    TRUE if OK, else FALSE
  111. // --------------------------------------------------------------------
  112.    static BOOL NEAR ViewerInitAppl( VOID )
  113.  
  114.      {WNDCLASS wc;                     // Window class information
  115.  
  116.    // Register the frame (main) window class
  117.  
  118.       wc.style         = 0;
  119.       wc.lpfnWndProc   = ViewerFrameWndProc;
  120.       wc.cbClsExtra    = 0;
  121.       wc.cbWndExtra    = 0;
  122.       wc.hInstance     = g.hInstance;
  123.       wc.hIcon         = LoadIcon( g.hResources,
  124.                                   MAKEINTRESOURCE( VIEWER_VIEWER_ICON ));
  125.       wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  126.       wc.hbrBackground = (HBRUSH) ( COLOR_APPWORKSPACE + 1 );
  127.       wc.lpszMenuName  = NULL;
  128.       wc.lpszClassName = VIEWER_FRAME_CLASS;
  129.  
  130.       if( !RegisterClass( &wc ))
  131.           return FALSE;
  132.  
  133.    // Register the picture window class
  134.  
  135.       wc.style         = 0;
  136.       wc.lpfnWndProc   = ViewerPictureWndProc;
  137.       wc.cbClsExtra    = 0;
  138.       wc.cbWndExtra    = sizeof( VOID NEAR * );
  139.       wc.hInstance     = g.hInstance;
  140.       wc.hIcon         = NULL;
  141.       wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  142.       wc.hbrBackground = PICBACKGRNDBRUSH;
  143.       wc.lpszMenuName  = NULL;
  144.       wc.lpszClassName = VIEWER_PICTURE_CLASS;
  145.  
  146.       if( !RegisterClass( &wc ))
  147.           return FALSE;
  148.  
  149.    // Register the child classes used by the picture windows
  150.    // This function is in PictKids.c
  151.  
  152.       if( !RegisterChildControls( g.hInstance ))
  153.           return FALSE;
  154.  
  155.       return TRUE;
  156.      }
  157.  
  158.  
  159. // Function: ViewerInitInst - Perform Instance Initialization
  160. // --------------------------------------------------------------------
  161. // Parameters: HINSTANCE hPrevInstance;  Previous instance
  162. //             LPSTR     lpCmdLine;      -->Command line arguments
  163. //             int       nCmdShow;       Parameter for first ShowWindow()
  164. //
  165. // Returns:    HWND      hwndFrame;      Frame window handle
  166. //                                       or NULL if initialization failed
  167. // --------------------------------------------------------------------
  168.    static HWND NEAR ViewerInitInst
  169.                  ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  170.  
  171.      {HDC hdcIC;
  172.  
  173.    // Do QuickTime Initializations.
  174.       if( !DoQuickTimeInit( hPrevInstance, lpCmdLine, &nCmdShow ))
  175.           return NULL;
  176.  
  177.    // Is this a palettized device?
  178.       if( hdcIC = CreateIC( "DISPLAY", NULL, NULL, NULL ))
  179.          {g.fPalettized = 
  180.                       ( GetDeviceCaps( hdcIC, RASTERCAPS ) & RC_PALETTE );
  181.           DeleteDC( hdcIC );
  182.          }
  183.  
  184.    // get menu and accelerators from localized resource
  185.       g.hAccel = LoadAccelerators( g.hResources,
  186.                                   MAKEINTRESOURCE( VIEWER_ACCELERATORS ));
  187.       g.hMenu  = LoadMenu( g.hResources,
  188.                                     MAKEINTRESOURCE( VIEWER_FRAME_MENU ));
  189.       if( !g.hAccel || !g.hMenu )
  190.          {CommonTellUser( g.hResources, VIEWER_STRING_NOACCELORMENU,
  191.                                           VIEWER_STRING_CAPTION, MB_OK );
  192.           return NULL;
  193.          }
  194.  
  195.    // Create a main window for this application instance.
  196.       if( !(g.hwndFrame = CreateWindow( VIEWER_FRAME_CLASS, "",
  197.                   WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  198.                   CW_USEDEFAULT, CW_USEDEFAULT,
  199.                   CW_USEDEFAULT, CW_USEDEFAULT,
  200.                   NULL, g.hMenu, g.hInstance, NULL )))
  201.          {CommonTellUser( g.hResources, VIEWER_STRING_NOWINDOW,
  202.                                        VIEWER_STRING_CAPTION, MB_OK );
  203.           return NULL;
  204.          }
  205.  
  206.    // Tell qtole.dll the server hwnd
  207.       QTOLE_SetApplicationHwnd( &g.qtoleOleData, g.hwndFrame ); 
  208.  
  209.    // save MDI client window created during WM_CREATE message processing
  210.       g.hwndClient = ViewerQueryClientWindow();   // this is in FrameWnd.c
  211.  
  212.    // Display the frame window
  213.    // If ole started the app, the window will be initially hidden
  214.       ShowWindow( g.hwndFrame, nCmdShow );
  215.       if( nCmdShow != SW_HIDE )
  216.           UpdateWindow( g.hwndFrame );
  217.  
  218.    // Check command line for picture file.
  219.    // Note: this must come after ShowWindow, UpdateWindow
  220.       if( lpCmdLine = ViewerParseCmdLine( lpCmdLine ))
  221.           SendMessage( g.hwndFrame, 
  222.                        WM_VIEWER_CMDLINE, 0, (LPARAM) lpCmdLine );
  223.  
  224.       return g.hwndFrame;
  225.      }
  226.  
  227. // Function: ViewerParseCmdLine - Parse the command line. Return -> to
  228. //                                first argument and ignore any extras.
  229. //                                The default extension is added if there
  230. //                                is no extension
  231. // --------------------------------------------------------------------
  232. // Parameters: LPSTR      lpCmdLine     command line pointer
  233. //
  234. // Returns:    LPSTR      lpCmdLine     command line pointer to first
  235. //                                      argument, else NULL
  236. // --------------------------------------------------------------------
  237.    static LPSTR NEAR ViewerParseCmdLine( LPSTR lpCmdLine )
  238.  
  239.      {LPSTR  lpTemp;                          //  Temp pointer
  240.       char   szExtension[FILE_EXT_LEN + 1];   // Default file extension
  241.       BOOL   bExtension;                      // Extension flag
  242.  
  243.    // Command line is already in Ansi char set even if entered from
  244.    // DOS command line
  245.  
  246.    // remove any leading blanks
  247.       while( *lpCmdLine == ' ' )
  248.           lpCmdLine = AnsiNext( lpCmdLine );
  249.  
  250.       if( *lpCmdLine == '\0' )
  251.           return NULL;
  252.  
  253.    // look for blank or end of string
  254.       bExtension = FALSE;
  255.       lpTemp = lpCmdLine;
  256.       while( *lpTemp && (*lpTemp != ' ' ))
  257.          {if( *lpTemp == '.' )
  258.               bExtension = TRUE;
  259.           lpTemp = AnsiNext( lpTemp );
  260.          }
  261.  
  262.       *lpTemp = '\0';
  263.  
  264.       if( !bExtension )
  265.          {LoadString( g.hResources,
  266.                 VIEWER_STRING_FILEEXT, szExtension, sizeof( szExtension ));
  267.           lstrcat( lpCmdLine, szExtension );
  268.          }
  269.  
  270.       return lpCmdLine;
  271.      }
  272.  
  273.  
  274. // Function: ViewerTerminateInst - Terminate Instance
  275. // --------------------------------------------------------------------
  276. // Parameters: VOID
  277. //
  278. // Returns:    Always 0L
  279. // --------------------------------------------------------------------
  280.    static LONG NEAR ViewerTerminateInst( VOID )
  281.  
  282.      {
  283.    // Clean up OLE
  284.       if( g.qtoleOleData.lpqtoleServer )
  285.           QTOLE_OLECleanUp( &g.qtoleOleData );
  286.  
  287.    // Cut the connections to QuickTime.
  288.       KillQuickTime();
  289.  
  290.    // Free the resource-only DLL
  291.       if( g.hResources && ( g.hInstance != g.hResources ))
  292.          FreeLibrary( g.hResources );
  293.  
  294.       return 0L;
  295.      }
  296.  
  297.  
  298. // The next two functions are used to initialize and kill QuickTime
  299.  
  300. // Function: DoQuickTimeInit - Establishes connections to QuickTime
  301. // --------------------------------------------------------------------
  302. // Parameters: HINSTANCE  hPrevInstance  Previous instance
  303. //             LPSTR      lpCmdLine      -> command line
  304. //             LPINT      lpnCmdShow     Parameter for first ShowWindow()
  305. //
  306. // Returns:    BOOL       TRUE if OK, else FALSE
  307. // --------------------------------------------------------------------
  308.    static BOOL NEAR DoQuickTimeInit
  309.                ( HINSTANCE hPrevInstance, LPSTR lpCmdLine, LPINT lpnCmdShow )
  310.  
  311.      {OSErr       oserr;                // Temp return value
  312.       WORD        wIDString;            // ID of error message string
  313.       QTOLE_INIT  qtoleInit;
  314.       QTOLE_ERR   qtole_err;
  315.       char        szCaption[30];
  316.  
  317.       LoadString( g.hResources, VIEWER_STRING_CAPTION,
  318.                                        szCaption, sizeof( szCaption ));
  319.  
  320.          // Do OLE initialization before QTInitialize so we
  321.          // don't have to kill qt for install only
  322.       if( !( qtoleInit.fpServerCallBack = 
  323.                     (QTOLEPROC) MakeProcInstance
  324.                               ( (FARPROC) QTOLEServerCallBack, g.hInstance )))
  325.          {CommonTellUser( g.hResources, VIEWER_STRING_NOMEMORY,
  326.                                            VIEWER_STRING_CAPTION, MB_OK );
  327.           return FALSE;
  328.          }
  329.       else
  330.          {qtoleInit.lStructSize           = sizeof( qtoleInit );
  331.           qtoleInit.lVersion              = VERSION_1;
  332.           qtoleInit.hInstance             = g.hInstance;
  333.           qtoleInit.hResources            = g.hResources;
  334.           qtoleInit.lpCmdLine             = lpCmdLine;
  335.           qtoleInit.lpClassName           = VIEWER_FRAME_CLASS;
  336.           qtoleInit.lpServerCaption       = szCaption;
  337.           qtoleInit.lpnCmdShow            = lpnCmdShow;
  338.           qtoleInit.wIDFirstString        = OLE_STRING_FIRST;
  339.           qtoleInit.wIDFirstDlg           = OLE_DLG_FIRST;
  340.           qtoleInit.bMultipleObjectServer = TRUE;
  341.                      
  342.           g.qtoleOleData.lStructSize      = sizeof( g.qtoleOleData );
  343.           g.qtoleOleData.lVersion         = VERSION_1;
  344.           g.qtoleOleData.wObjectType      = PICTURE_OBJECT;
  345.  
  346.           if( QTOLE_OK != 
  347.                    ( qtole_err = QTOLE_Initialize( &g.qtoleOleData, &qtoleInit )))
  348.              {if( QTOLE_INSTALL_ONLY != qtole_err )
  349.                  CommonTellUser( g.hResources, 
  350.                             VIEWER_STRING_OLEINITFAILED,
  351.                                            VIEWER_STRING_CAPTION, MB_OK );
  352.               return FALSE;
  353.              }
  354.          }
  355.  
  356.       if( ( oserr = QTInitialize( NULL )) != QTI_OK )
  357.          {switch( oserr )
  358.              {case QTI_FAIL_NOEXIST:
  359.                  wIDString = VIEWER_STRING_QTWNOEXIST;
  360.                  break;
  361.  
  362.               case QTI_FAIL_CORRUPTDLL:
  363.                  wIDString = VIEWER_STRING_QTWBADDLL;
  364.                  break;
  365.  
  366.               case QTI_FAIL_286:
  367.                  wIDString = VIEWER_STRING_QTW286;
  368.                  break;
  369.  
  370.               case QTI_FAIL_WIN30:
  371.                  wIDString = VIEWER_STRING_QTWWIN30;
  372.                  break;
  373.  
  374.               default:
  375.                  wIDString = VIEWER_STRING_QTWFAILED;
  376.                  break;
  377.              }
  378.  
  379.           CommonTellUser( g.hResources, wIDString,
  380.                                         VIEWER_STRING_CAPTION, MB_OK );
  381.           return FALSE;
  382.          }
  383.  
  384.         // This function call is necessary to make possible the use of
  385.         // the function GetMovieStickyError() that returns the error code
  386.         // for certain QTW functions.
  387.       if( EnterMovies() )
  388.          {CommonTellUser( g.hResources, VIEWER_STRING_ENTMOVFAILED,
  389.                                            VIEWER_STRING_CAPTION, MB_OK );
  390.           return FALSE;
  391.          }
  392.  
  393.       return TRUE;
  394.      }
  395.  
  396.  
  397. // Function: KillQuickTime - Cuts the connections to QuickTime
  398. // --------------------------------------------------------------------
  399. // Parameters: VOID
  400. //
  401. // Returns:    VOID
  402. // --------------------------------------------------------------------
  403.    static VOID NEAR KillQuickTime( VOID )
  404.  
  405.      {ExitMovies();
  406.       QTTerminate();
  407.  
  408.       return;
  409.      }
  410.  
  411.  
  412. //  This function is called by other modules whenever a modeless dialog
  413. //  is activated or deactivated
  414.  
  415. // Function: ViewerSetActiveModeless - Set handle of active modeless dlg
  416. // --------------------------------------------------------------------
  417. // Parameters: WPARAM   wParam          WA_ flag defined in Windows.h
  418. //             HWND     hModelessDlg    Handle of dlg being activated or
  419. //                                      deactivated
  420. //
  421. // Returns:    VOID
  422. // --------------------------------------------------------------------
  423.    VOID FAR ViewerSetActiveModeless( WPARAM wParam, HWND hModelessDlg )
  424.  
  425.      {if( wParam != WA_INACTIVE )
  426.           g.hActiveModelessDlg = hModelessDlg;
  427.       else if( g.hActiveModelessDlg == hModelessDlg )
  428.           g.hActiveModelessDlg = NULL;
  429.  
  430.       return;
  431.      }
  432.  
  433. // Function: ViewerNoMoreWindow - Sets global handles to NULL. Called during
  434. //                                frame window WM_DESTROY message processing
  435. //                                This is needed for accelerator tests in 
  436. //                                main message loop after window is destroyed
  437. // --------------------------------------------------------------------
  438. // Parameters: VOID
  439. //
  440. // Returns:    VOID
  441. // --------------------------------------------------------------------
  442.    VOID FAR ViewerNoMoreWindow( VOID )
  443.  
  444.      {g.hwndFrame  = NULL;
  445.       g.hwndClient = NULL;
  446.  
  447.       return;
  448.      }
  449.  
  450.  
  451. ///  The remaining functions are the query functions called by other modules
  452.  
  453. // Function: ViewerQueryInstance - Query Instance Handle
  454. // --------------------------------------------------------------------
  455. // Parameters: None.
  456. //
  457. // Returns:    HINSTANCE hInstance;    Application instance handle
  458. // --------------------------------------------------------------------
  459.    HINSTANCE FAR ViewerQueryInstance( VOID )
  460.  
  461.      {return g.hInstance;
  462.      }
  463.  
  464. // Function: ViewerQueryResources - Query Resource-Only DLL Handle
  465. // --------------------------------------------------------------------
  466. // Parameters: None.
  467. //
  468. // Returns:    HINSTANCE hResources    Resource-only DLL handle
  469. // --------------------------------------------------------------------
  470.    HINSTANCE FAR ViewerQueryResources( VOID )
  471.  
  472.      {return g.hResources;
  473.      }
  474.  
  475. // Function: ViewerQueryFrameWindow - Query Frame Window Handle
  476. // --------------------------------------------------------------------
  477. // Parameters: None.
  478. //
  479. // Returns:    HWND hwndFrame;          Frame window handle
  480. // --------------------------------------------------------------------
  481.    HWND FAR ViewerQueryFrameWindow( VOID )
  482.  
  483.      {return g.hwndFrame;
  484.      }
  485.  
  486. // Function: ViewerIsPalettized - Are we running on a palettized device?
  487. // --------------------------------------------------------------------
  488. // Parameters: None.
  489. //
  490. // Returns:    BOOL fPalettized;        TRUE if palettized device
  491. // --------------------------------------------------------------------
  492.    BOOL FAR ViewerIsPalettized( VOID )
  493.  
  494.      {return g.fPalettized;
  495.      }
  496.  
  497. // Function: ViewerQueryOleData - Query -> to Ole Data struct
  498. // --------------------------------------------------------------------
  499. // Parameters: None.
  500. //
  501. // Returns:    LPQTOLE_OLEDATA        -> ole data struct
  502. // --------------------------------------------------------------------
  503.    LPQTOLE_OLEDATA FAR ViewerQueryOleData( VOID )
  504.  
  505.      {return &g.qtoleOleData;
  506.      }
  507.  
  508.  
  509.